//
//  TFkeyboardTool.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/12.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TFkeyboardTool: UIView {
    //表情选项view
    @objc let expressionView = TF1to16ExpressionView()
  //常用语选项view
    @objc let commonPhrasesView = TFmmonPhrasesView()
   //常用语 标题
    @objc let commonLanguage = UIButton()
    @objc let topView = UIView()
   //输入栏
    @objc let inputField = UITextView()
    //提问栏
    @objc let qskQuestions = UITextView()
  //发送
    @objc let longSendButton = TFCenteredleLabel()
   //表情
    @objc let expressionButton = UIButton()
   //提问的花
    @objc let flower = TFMultifunctionButton()
    
//    常用语按键
    @objc func commonLanguageClick(btn:UIButton)
    {
        self.click(btn: btn)
      
        if (btn.isSelected) {
            //隐藏表情图标
            self.expressionView.isHidden = true
            
            self.expressionButton.isSelected = false
            self.expressionButton.setImage(UIImage.init(named: "HTLive.bundle/huisebiaoqing"), for: .normal)
           
            //常用语显示
            self.commonPhrasesView.commonPhrases.removeAllObjects()
     
            let a1 = "你能看到我画面吗"
            let a2 = "你能听到我声音吗"
            let a3 = "我看不到你的画面?"
            let a4 = "我听不到你的声音?"
            
            self.commonPhrasesView.commonPhrases.add(a1)
            self.commonPhrasesView.commonPhrases.add(a2)
            self.commonPhrasesView.commonPhrases.add(a3)
            self.commonPhrasesView.commonPhrases.add(a4)
          
            self.commonPhrasesView.isHidden = false
            self.commonPhrasesView.collectionView?.reloadData()
           
        }else{
            //常用文字
            self.setfCommonLanguageTitle()
          
            //常用语显示隐藏
            self.commonPhrasesView.isHidden = true
            self.inputField.becomeFirstResponder()
           
           
        }
    }
    
    //表情事件
    @objc func expressionButtonClick(btn:UIButton)
    {
        self.click(btn: btn)
        if (btn.isSelected) {
            //常用语初始
            self.expressionButton.setImage(UIImage.init(named: "HTLive.bundle/wenben"), for: .normal)
          
            //显示表情view
            self.expressionView.isHidden = false
            //常用语隐藏
            self.commonPhrasesView.isHidden = true
           
        }else{
            self.expressionButton.setImage(UIImage.init(named: "HTLive.bundle/huisebiaoqing"), for: .normal)
           
            //隐藏表情view
            self.expressionView.isHidden = true
            //切换回输入栏
            self.inputField.becomeFirstResponder()
           

        }
        self.setfCommonLanguageTitle()
     
    }
    @objc func click(btn:UIButton)
    {
        btn.isSelected = !btn.isSelected
        var UIScreenHeight:CGFloat = UIScreen.main.bounds.height

        
        if(  UIScreen.main.bounds.width <  UIScreen.main.bounds.height ){
            UIScreenHeight =  UIScreen.main.bounds.size.width

        }
        self.endEditing(true)

        self.frame = CGRect(x: self.frame.origin.x, y: UIScreenHeight-217, width: self.frame.size.width, height: 217)
       
    }
    @objc func setfCommonLanguageTitle()
    {
        self.commonLanguage.isSelected = false
        self.commonLanguage.setImage(UIImage.init(named: "HTLive.bundle/"), for: .normal)
        self.commonLanguage.setTitle("常用语", for: .normal)
        self.commonLanguage.setTitleColor(UIColor(red: 76/255.0, green: 81/255.0, blue: 88/255.0, alpha: 1), for: .normal)
        
        self.commonLanguage.titleLabel!.font = UIFont.PingFang(size: 16)
        
        self.commonLanguage.layer.borderColor = UIColor(red: 236/255.0, green: 237/255.0, blue: 239/255.0, alpha: 1).cgColor
        self.commonLanguage.layer.borderWidth = 0.5
        self.commonLanguage.layer.cornerRadius = 22.5
    }
   
    override init(frame: CGRect) {
        super.init(frame: frame)

        self.topView.frame = CGRect(x: 0, y: 569, width: 1024, height: 75)

        
        self.topView.layer.borderColor = UIColor(red: 236/255.0, green: 236/255.0, blue: 236/255.0, alpha: 1).cgColor

        self.topView.layer.borderWidth = 0.5
        self.topView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        self.addSubview(self.topView)
        
        
         //常用语
        self.commonLanguage.addTarget(self, action: #selector(self.commonLanguageClick), for: .touchUpInside)
        
        self.topView.addSubview(self.commonLanguage)
        
        //TODO:发送
        self.longSendButton.backgroundColor = UIColor(red: 100/255.0, green: 152/255.0, blue: 255/255.0, alpha: 1)
        self.longSendButton.setTitle("发送", for: .normal)
        
         self.longSendButton.titleLabel!.font = UIFont.PingFang(size: 18)

        self.longSendButton.layer.masksToBounds = true
        self.longSendButton.layer.cornerRadius = 4
        
        self.topView.addSubview(self.longSendButton)
        
        //TODO:表情图标
        self.expressionButton.setImage(UIImage.init(named: "HTLive.bundle/huisebiaoqing"), for: .normal)
        self.expressionButton.addTarget(self, action: #selector(self.expressionButtonClick), for: .touchUpInside)
        
        self.expressionButton.backgroundColor = .clear
        self.topView.addSubview(self.expressionButton)
        
        
        
        //TODO:表情图标
       self.flower.flower = true
       self.flower.isHidden = true
       self.flower.setImage(UIImage.init(named: "HTLive.bundle/freshFlowers1to16"), for: .normal)
        self.topView.addSubview(self.flower)
        

        self.topView.addSubview(self.inputField)
        self.inputField.textColor = .black
        
        self.inputField.backgroundColor = UIColor(red: 244/255.0, green: 244/255.0, blue: 244/255.0, alpha: 1)
        self.inputField.layer.cornerRadius = 3;

        //表情

        self.expressionView.expressionBlock  = {[weak self] str in
                guard let `self` = self else { return }
           
                self.inputField.text = self.inputField.text + str
                
            
        }
        self.expressionView.delExpressionBlock  = {[weak self] str in
                guard let `self` = self else { return }
           
            let instr:NSString = self.inputField.text! as NSString
            
            self.inputField.text = TalkfunSharedTools.GetTextMessage(instr) as String
                
            
        }
    
        self.expressionView.backgroundColor = .clear
        self.addSubview(self.expressionView)
        
        //常用语
        self.commonPhrasesView.backgroundColor = UIColor.clear
        self.addSubview(self.commonPhrasesView)
        
        self.qskQuestions.isHidden = true
        self.topView.addSubview(self.qskQuestions)
        self.qskQuestions.textColor = UIColor.black
        
        self.qskQuestions.backgroundColor = UIColor(red: 244/255.0, green: 244/255.0, blue: 244/255.0, alpha: 1)
       self.qskQuestions.layer.cornerRadius = 3
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        self.topView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 70)

        //表情选项
        self.expressionView.frame = CGRect(x: 0, y: self.topView.frame.size.height+5, width: self.frame.size.width, height: self.frame.size.height-self.topView.frame.size.height)
       
        //常用语
        self.commonPhrasesView.frame = CGRect(x:0 , y: self.topView.frame.size.height+5, width: self.frame.size.width, height: self.frame.size.height-self.topView.frame.size.height)
       let rect = TalkfunSharedTools.getRectWith("常用语", size: CGSize(width: 200, height: 40), fontSize: 16)
        
        let commonLanguageW:CGFloat = rect.size.width+27 //75;
        let commonLanguageH:CGFloat = 40
        let commonLanguageX:CGFloat = 25;
        let commonLanguageY:CGFloat = (self.topView.frame.size.height - commonLanguageH)/2;
        
        //常用语 标题
        self.commonLanguage.frame = CGRect(x: commonLanguageX, y: commonLanguageY, width: commonLanguageW, height: commonLanguageH)
        
        if( self.commonLanguage.isSelected){
            self.commonLanguage.setImage(UIImage.init(named: "HTLive.bundle/wenben"), for: .normal)
            self.commonLanguage.setTitle("", for: .normal)
          
            self.commonLanguage.layer.borderWidth = 0
            self.commonLanguage.layer.cornerRadius = 0
        }else{
            self.setfCommonLanguageTitle()
         
        }
        //间距
        let bb:CGFloat = 25;
        let longSendButtonW:CGFloat = 90;
        let longSendButtonH:CGFloat = 45;
        let longSendButtonX:CGFloat = self.topView.frame.size.width-longSendButtonW-bb
        let longSendButtonY:CGFloat = (self.topView.frame.size.height-longSendButtonH)/2

       //发送
        self.longSendButton.frame = CGRect(x: longSendButtonX, y: longSendButtonY, width: longSendButtonW, height: longSendButtonH)
        
        self.longSendButton.setTitle("发送", for: .normal)
       
        let expressionButtonH:CGFloat = 30;
        let expressionButtonW:CGFloat = expressionButtonH;
        let expressionButtonX:CGFloat =  longSendButtonX - expressionButtonW-bb;
      
        let expressionButtonY:CGFloat = (self.topView.frame.size.height - expressionButtonH)/2;
        
        
        self.expressionButton.frame = CGRect(x: expressionButtonX, y: expressionButtonY, width: expressionButtonW, height: expressionButtonH)
       
        //隐藏了
        if (self.flower.isHidden) {
            self.flower.frame = self.expressionButton.frame
        }else{
            self.flower.frame = CGRect(x:self.expressionButton.frame.origin.x-expressionButtonW-bb, y: (self.topView.frame.size.height - expressionButtonH)/2, width: expressionButtonW, height: expressionButtonW)
        }

      
        let inputFieldH:CGFloat = 35;

        let inputFieldX:CGFloat = (commonLanguageX+commonLanguageW+bb)
        
        var righ:CGFloat = (longSendButtonW+bb)+(expressionButtonW+bb)
        //有花
        if (self.flower.isHidden == false) {
            righ = righ+(expressionButtonW+bb)
        }
        let inputFieldW = self.topView.frame.size.width - inputFieldX - righ - bb
        
        
        let inputFieldY = (self.topView.frame.size.height - inputFieldH)/2
        self.inputField.frame = CGRect(x: inputFieldX, y: inputFieldY, width: inputFieldW, height: inputFieldH)
      
        
        self.qskQuestions.frame = CGRect(x: 10, y: inputFieldY, width: self.longSendButton.frame.origin.x-20, height: inputFieldH)
       
        ///下面\
        if (self.frame.size.height>70)
        {
            if (self.expressionButton.isSelected==false) {
                self.commonPhrasesView.isHidden = false
                //重新设置成表情 图标
                self.expressionButton.setImage(UIImage.init(named: "HTLive.bundle/huisebiaoqing"), for: .normal)
               

            }else{
                
               
                 self.commonPhrasesView.isHidden = true
            }
           
        }
    }

}
